package licode.unisop.authcenter.adapter.web;

import licode.unisop.authcenter.application.executor.CenterPlatformExecutor;
import licode.unisop.client.inter.HasAuthority;
import licode.unisop.platform.client.info.PlatformSaveIn;
import licode.unisop.provider.info.SiAuthPlatToUserIn;
import licode.unisop.provider.info.SiPlatformId;
import licode.unisop.provider.info.SiUserId;
import licode.unisop.provider.vo.SiPlatform;
import licode.unisop.types.AuthConst;
import licode.unisop.unilite.result.UniReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 定义统一认证平台中心领域相关接口
 *
 * @author licode
 */
@Slf4j
@UniReturnResult
@RestController
@RequestMapping("/uni-center/v1/platform")
public class CenterPlatformController extends CenterBaseController {
    @Resource
    private CenterPlatformExecutor centerPlatformExecutor;

    @RequestMapping(value = "/cache/update", method = RequestMethod.POST)
    @HasAuthority(roles = AuthConst.SYSTEM_ADMIN, permits = "plat:update")
    public void updateCache(@RequestBody SiPlatformId platId) {
        centerPlatformExecutor.updateCache(platId);
    }

    /**
     * 增加平台信息
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @HasAuthority(roles = AuthConst.SYSTEM_ADMIN, permits = "plat:add")
    public void add(@RequestBody SiPlatform item) {
        centerPlatformExecutor.add(PlatformSaveIn.builder()
                .authIn(getAuthIn())
                .platform(item)
                .build());
    }

    /**
     * 保存平台信息
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @HasAuthority(roles = AuthConst.SYSTEM_ADMIN, permits = "plat:update")
    public void update(@RequestBody SiPlatform item) {
        centerPlatformExecutor.update(PlatformSaveIn.builder()
                .authIn(getAuthIn())
                .platform(item)
                .build());
    }

    /**
     * 授权平台信息
     */
    @RequestMapping(value = "/grant-to-user", method = RequestMethod.POST)
    @HasAuthority(roles = AuthConst.SYSTEM_ADMIN, permits = "plat:update")
    public void grantPlatform(@RequestBody SiAuthPlatToUserIn item) {
        item.setOwnerId(AuthConst.SSO_MANAGER_PLATFORM_ID);
        centerPlatformExecutor.grantPlatform(item);
    }
}
